/*
*
*  用户学习数据模型
*  
*
*/
// 导入处理数据库查询的文件
const db = require('../../config/db.js')
module.exports = {
    // 编写用户学习方法
    // 参数：practice_id, user_id, practice_create_time,practice_num
    add(practice_id, user_id, practice_create_time, practice_num) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_practice where practice_id='" + practice_id + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '学习查询失败。' })
                } else {
                    if (rows.length == 0) {
                        db.query("insert into db_practice(practice_id,user_id,practice_create_time,practice_num) values('" + practice_id + "','" + user_id + "','" + practice_create_time + "','"+practice_num+"')", (err, rows) => {
                            if (err) {
                                resolve({ code: -1, msg: '添加学习失败。' })
                            } else {
                                resolve({ code: 200, msg: '添加学习成功。' })
                            }
                        })
                    } else {
                        resolve({ code: 201, msg: '已学习。', practice_status: rows['practice_status'] })
                    }
                }
            })
        })
    },
    // 编写查询用户学习记录方法
    // 参数：practice_id,user_id,practice_create_time,practice_status
    list(user_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_practice where user_id = '" + user_id + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '学习查询失败。' })
                } else {
                    resolve({ code: 200, msg: '未学习。', rseult:rows })
                }
            })
        })
    },

    // 编写删除学习方法
    // 参数：practice_id
    remove(practice_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_practice where practice_id='" + practice_id + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '查询学习失败。' })
                } else {
                    db.query("delete from  db_practice where practice_id = '" + practice_id + "'", function (err, rows) {
                        if (err) {
                            resolve({ code: -1, msg: '删除学习失败。' })
                        } else {
                            resolve({ code: 200, msg: '删除学习成功。' })
                        }
                    })
                }
            })
        })
    },
    // 编写统计用户学习次数方法
    // 参数：user_id
    total(user_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_practice where user_id='" + user_id + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '学习查询次数失败。' })
                } else {
                    if (rows.length == 0) {
                        resolve({ code: 200, msg: '查询学习次数。', total: 0 })
                    } else {
                        let count = 0;
                        for (let i = 0; i < rows.length; i++) { count++; }
                        resolve({ code: 200, msg: '查询学习次数。', total: count })
                    }
                }
            })
        })
    },
    // 编写学习分页列表方法
    // 参数：page_num,page_size
    items(page_num, page_size) {
        return new Promise((resolve, reject) => {
            const params = [(parseInt(page_num) - 1) * parseInt(page_size), parseInt(page_size)]
            db.query("select * from db_practice limit " + params[0] + "," + params[1] + "", function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询学习失败。',
                        err
                    })
                } else {
                    //as更换名称
                    db.query('select count(*) as total from db_practice', function (error, among) {
                        if (error) {
                            resolve({
                                code: -1,
                                msg: '统计学习数量失败。',
                                error
                            })
                        } else {
                            resolve({
                                code: 200,
                                msg: "获取学习列表成功。",
                                result: rows,
                                page: {
                                    page_num: page_num,
                                    page_size: page_size,
                                    total: among[0]['total'] //查询表中的数量
                                }
                            })
                        }
                    })
                }
            })

        })
    },
    // 编写统计学习方法
    all(user_id) {
        return new Promise((resolve, reject) => {
            let sql = "select count(*) as total from db_practice where user_id = '" + user_id + "'";
            // console.log(sql);
            db.query(sql, function (error, among) {
                if (error) {
                    resolve({
                        code: -1,
                        msg: '统计学习数量失败。',
                        error
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取用户学习次数。",
                        total: among[0]['total']
                    })
                }
            })
        })
    },
    
}


